import { AuthRole, ILoginParam } from "@/headers/admin/auth.h";
import { ICheckRoleRes, ReqApiUrls } from "@/headers/admin/reqs.h";
import { IAdminStore, IAdminUserInfo } from "@/headers/admin/store.h";
import {apiAxios as axios} from "@/utils/axios";
import { Store, ActionContext,ActionTree } from "vuex";
import { ActionBase, ActionCache, registerAll } from "./common";
import { ChangeUserInfoMutation } from "./mutation";
import { initAdminClubAction } from "./actions/club";
import { initAssetsAction } from "./actions/assets";
import { initUserAction } from "./actions/user";


// auth


export class LoginAction extends ActionBase<IAdminStore,IAdminStore,ILoginParam,IAdminUserInfo|null> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: ILoginParam): Promise<IAdminUserInfo> {
        const {data} = await axios.post<IAdminUserInfo>(ReqApiUrls.AccountAndPwd,payload)
        
        ChangeUserInfoMutation.getInstance().call(store,data)

        return data
    }
    
}

export class LogoutAction extends ActionBase<IAdminStore,IAdminStore,void,boolean> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, _payload: void): Promise<boolean> {
        const {data} = await axios.delete<boolean>(ReqApiUrls.SelfInfo);
        ChangeUserInfoMutation.getInstance().call(store,null);
        return data;
    }
}

export class GetUserInfoAction extends ActionBase<IAdminStore,IAdminStore,void,IAdminUserInfo> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: void): Promise<IAdminUserInfo> {
        const {data} = await axios.get<IAdminUserInfo>(ReqApiUrls.SelfInfo);

        ChangeUserInfoMutation.getInstance().call(store,data);

        return data;
    }
    
}

export class GetUserInfoActionCache extends ActionBase<IAdminStore,IAdminStore,void,IAdminUserInfo> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: void): Promise<IAdminUserInfo> {
        if(store.state.userInfo) {
            return store.state.userInfo;
        }
        return await GetUserInfoAction.getInstance().call(store,payload);
    }
    
}

export class GetUserActorRoleAction extends ActionCache<IAdminStore,IAdminStore,void,number> {
    public async handle(store: Store<IAdminStore>, ctx: ActionContext<IAdminStore, IAdminStore>, payload: void): Promise<number> {
        const {data} = await axios.get<ICheckRoleRes>(ReqApiUrls.CheckRole)
        let res = AuthRole.Guest;
        if(data.student){
            res |= AuthRole.Student;
        }
        if(data.leader){
            res |= AuthRole.Leader;
        }
        if(data.admin) {
            res |= AuthRole.Admin;
        }
        return res;
    }

}





const adminAction:ActionTree<any,any> = {};


registerAll(
    adminAction,
    LoginAction,
    LogoutAction,
    GetUserInfoAction,
    GetUserActorRoleAction,
    GetUserInfoActionCache
);

initAdminClubAction(adminAction);
initAssetsAction(adminAction);
initUserAction(adminAction);


export const AdminAction = adminAction;
